package com.gitee.wsl.collections.iterator


@Suppress("UNCHECKED_CAST")
class IndexedIterator<T>(
    private var size: () -> Int,
    private val getter: (Int) -> T,
    private val eraser: (Int) -> Unit
) : MutableIterator<T> {
    private var index = 0
    private var length = size()

    override fun hasNext(): Boolean {
        return index < length
    }

    override fun next(): T {
        return getter(index++)
    }

    override fun remove() {
        eraser(--index)
        length = size()
    }
}